<?php

/**
 * 附件上传
 */
namespace Asset\Controller;
use Admin\Controller\Tencentyun\ImageV2;
use Common\Controller\AdminbaseController;

class AssetController extends AdminbaseController
{


    function _initialize()
    {
        $adminid = sp_get_current_admin_id();
        $userid = sp_get_current_userid();
        if (empty($adminid) && empty($userid)) {
            exit("非法上传！");
        }
    }

    /**
     * swfupload 上传
     */
    public function swfupload()
    {
        if (IS_POST) {
            $savepath = date('Ymd') . '/';
            //上传处理类
            $config = array(
                'rootPath' => './' . C("UPLOADPATH"),
                'savePath' => $savepath,
                'maxSize' => 11048576,
                'saveName' => array('uniqid', ''),
                'exts' => array('jpg', 'gif', 'png', 'jpeg', "txt", 'zip'),
                'autoSub' => false,
            );
            $image_path   = SITE_PATH.'/data/upload/';
            if (!is_dir($image_path)) {
                mkdir($image_path,0777,true);
            }
            $upload = new \Think\Upload($config);//
            $info = $upload->upload();
            //开始上传
            if ($info) {
                //上传成功
                //写入附件数据库信息
                $first = array_shift($info);
                if (!empty($first['url'])) {
                    $url = $first['url'];
                } else {
                    $url = C("TMPL_PARSE_STRING.__UPLOAD__") . $savepath . $first['savename'];
                }
                $url=SITE_PATH.$url;
                $uploadRet = ImageV2::upload($url, 'vrlive');
                $image_url = $uploadRet['data']['downloadUrl'];
                $this->ajaxReturn(array('preview_url'=>1,'filepath'=>2,'url'=>$image_url,'name'=>$first['name'],'status'=>1,'message'=>'success'));
                echo "1," . $url . "," . '1,' . $first['name'];
                exit;
            } else {
                //上传失败，返回错误
                exit("0," . $upload->getError());
            }
        } else {
            $this->display(':swfupload');
        }
    }

    public function pupload()
    {
        if (IS_POST) {
            if (1==1) {
                $this->ajaxReturn(array('preview_url'=>1,'filepath'=>2,'url'=>'http://'.$_SERVER['HTTP_HOST'],'name'=>"abc",'status'=>1,'message'=>'success'));
            } else {
                //上传失败，返回错误
                exit("0," . $upload->getError());
            }
        } else {
            $this->display(':swfupload');
        }
    }
    
    /**
     * swfupload 上传
     */
    public function buycarupload()
    {
        if (IS_POST) {
            $carId = $_REQUEST['carId'];
            if (empty($carId) || $carId <= 0) {
                exit("0," . "请传入车辆Id");
                die();
            }
            $typeId = $_REQUEST['typeId'];
            if (empty($typeId) || $typeId <= 0) {
                exit("0," . "请传入类型Id");
                die();
            }
            $savepath = date('Ymd') . '/';
            //上传处理类
            $config = array(
                'rootPath' => './' . C("UPLOADPATH"),
                'savePath' => $savepath,
                'maxSize' => 11048576,
                'saveName' => array('uniqid', ''),
                'exts' => array('jpg', 'gif', 'png', 'jpeg', "txt", 'zip'),
                'autoSub' => false,
            );
            $upload = new \Think\Upload($config);//
            $info = $upload->upload();
            //开始上传
            if ($info) {
                //上传成功
                //写入附件数据库信息
                $first = array_shift($info);
                if (!empty($first['url'])) {
                    $url = $first['url'];
                } else {
                    $url = C("TMPL_PARSE_STRING.__UPLOAD__") . $savepath . $first['savename'];
                }
                $pictureId = 1;
                $maxOrder=0;
                $progressName="fsUploadProgress";
                if (!empty($url) && $carId > 0) {
                    if ($typeId == 1) {
                        $BuyCarModel = M("buy_cars_picture", 'sc_');
                        $maxOrder = $BuyCarModel->where("car_id={$carId}  and from_type=1 ")->order("display_order desc")->getField('display_order');
                        //\Think\Log::write($BuyCarModel->getLastSql(),'WARN');
                        if ($maxOrder < 0) {
                            $maxOrder = 0;
                        }
                        $maxOrder = $maxOrder + 1;
                        $data['car_id'] = $carId;
                        $data['type'] = 0;
                        $data['thumbnail_url'] = $url;
                        $data['original_url'] = $url;
                        $data['display_order'] = $maxOrder;
                        $data['from_type'] = 1;
                        $pictureId = $BuyCarModel->add($data);
                        //\Think\Log::write($BuyCarModel->getLastSql(),'WARN'); ;
                    } elseif ($typeId == 2) {
                        $RentCarPictureModel = M("rent_cars_picture", 'sc_');
                        $maxOrder = $RentCarPictureModel->where("car_id={$carId}")->order("display_order desc")->getField('display_order');
                        //\Think\Log::write($BuyCarModel->getLastSql(),'WARN');
                        if ($maxOrder < 0) {
                            $maxOrder = 0;
                        }
                        $maxOrder = $maxOrder + 1;
                        $data['car_id'] = $carId;
                        $data['type'] = 0;
                        $data['thumbnail_url'] = $url;
                        $data['original_url'] = $url;
                        $data['display_order'] = $maxOrder;
                        $pictureId = $RentCarPictureModel->add($data);
                    }elseif ($typeId == 3) {
                        $BuyCarModel = M("buy_cars_picture", 'sc_');
                        $maxOrder = $BuyCarModel->where("car_id={$carId}  and from_type=2 ")->order("display_order desc")->getField('display_order');
                        //\Think\Log::write($BuyCarModel->getLastSql(),'WARN');
                        if ($maxOrder < 0) {
                            $maxOrder = 0;
                        }
                        $maxOrder = $maxOrder + 1;
                        $data['car_id'] = $carId;
                        $data['type'] = 0;
                        $data['thumbnail_url'] = $url;
                        $data['original_url'] = $url;
                        $data['display_order'] = $maxOrder;
                        $data['from_type'] =2;
                        $pictureId = $BuyCarModel->add($data);
                        $progressName="fsUploadProgress2";
                    }
                }
                echo $pictureId . "," . $url . "," . '1,' . $maxOrder.",".$progressName;
                exit;
            } else {
                //上传失败，返回错误
                exit("0," . $upload->getError());
            }
        } else {
            $this->display(':buycarupload');
        }
    }

    public function delPicture($id)
    {
        $typeId = $_REQUEST['typeId'];
        if (empty($typeId) || $typeId <= 0) {
            $arr = array("status" => 0, "info" => '参数错误[type]');
            $this->ajaxReturn($arr);
        }
        if ($id <= 0) {
            $arr = array("status" => 0, "info" => '参数错误');
            $this->ajaxReturn($arr);
        }
        $result = false;
        if ($id > 0) {
            if ($typeId == 1) {
                $BuyCarPictureModel = M("buy_cars_picture", 'sc_');
                $result = $BuyCarPictureModel->where("picture_id={$id} and from_type=1 ")->delete();
            } elseif ($typeId == 2) {
                $RentCarPictureModel = M("rent_cars_picture", 'sc_');
                $result = $RentCarPictureModel->where("picture_id={$id}")->delete();
            }elseif ($typeId == 3) {
                $BuyCarPictureModel = M("buy_cars_picture", 'sc_');
                $result = $BuyCarPictureModel->where("picture_id={$id} and from_type=2 ")->delete();
            }
        }
        if ($result !== false) {
            $arr = array("status" => 1, "info" => '删除图片成功');
        } else {
            $arr = array("status" => 0, "info" => '非常抱歉，系统繁忙');
        }
        $this->ajaxReturn($arr);
    }

    public function updateSortOrder($id, $sort)
    {
        $typeId = $_REQUEST['typeId'];
        if (empty($typeId) || $typeId <= 0) {
            $arr = array("status" => 0, "info" => '参数错误[type]');
            $this->ajaxReturn($arr);
        }
        if ($id <= 0 || $sort <= 0) {
            $arr = array("status" => 0, "info" => '参数错误');
            $this->ajaxReturn($arr);
        }
        $result = false;
        if ($id > 0 && $sort > 0) {
            if ($typeId == 1) {
                $BuyCarPictureModel = M("buy_cars_picture", 'sc_');
                $result = $BuyCarPictureModel->where("picture_id={$id} and from_type=1")->save(array("display_order" => $sort));
            } elseif ($typeId == 2) {
                $RentCarPictureModel = M("rent_cars_picture", 'sc_');
                $result = $RentCarPictureModel->where("picture_id={$id}")->save(array("display_order" => $sort));
            }elseif ($typeId == 3) {
                $BuyCarPictureModel = M("buy_cars_picture", 'sc_');
                $result = $BuyCarPictureModel->where("picture_id={$id} and from_type=2")->save(array("display_order" => $sort));
            }
        }
        if ($result !== false) {
            $arr = array("status" => 1, "info" => '更新数据成功');
        } else {
            $arr = array("status" => 0, "info" => '非常抱歉，系统繁忙');
        }
        $this->ajaxReturn($arr);
    }
}
